var updateRoleDialog = new Dialog( {
	showdiv :"updateRoleDialog",
	title :"修改角色信息"
});
rootDialog.childDialog = updateRoleDialog;
var createRoleDialog = new Dialog( {
	showdiv :"createRoleDialog",
	title :"创建角色信息"
});
rootDialog.childDialog = createRoleDialog;
function initRole(roleid) {
	if (roleid != null && roleid != "" && roleid != -9) {
		Role.chakan(roleid, function(data) {
			for ( var i = 0; i < data.length; i++) {
				if (data[i].tname == 'sign' || data[i].tname == 'rolelevel') {
					DWRUtil.setValue(data[i].tname, data[i].tval);
				} else {
					$(data[i].tname).value = data[i].tvalue;
				}
			}
		});
	}
}

function newRole() {
	var role1 = trim_value(DWRUtil.getValue('rolename'));
	var sign = DWRUtil.getValue('sign');
	var roletext = trim_value(DWRUtil.getValue('roletext'));
	var roleid = DWRUtil.getValue('roleid');
	var rolelevel = DWRUtil.getValue('rolelevel');
	var checkpname = false;
	Role.checkPName(role1, roleid, function(data) {
		if (data == 1) {
			checkpname = true;
			$get("rolenameerror").innerHTML = "正确！";

		} else {
			checkpname = false;
			$get("rolenameerror").innerHTML = "角色名称已存在或包含非法字符！";
		}
		if (roletext.length > 120) {
			alert('描述不可超过120字');
			checkpname = false;
		}
		if (checkpname) {
			if (roleid != null && roleid != "") {
				Role.updateRole(roleid, role1, roletext, sign, rolelevel,
						function(data) {
							if (data == 1) {
								send(role);
								alert('角色修改成功！');
							} else {
								alert('角色修改失败！');
							}
							closeDialog(updateRoleDialog);
						});
			} else {
				Role.newRole(role1, roletext, sign, rolelevel, function(data) {
					if (data == 1) {
						send(role);
						alert('添加角色成功！');
					} else {
						alert('添加角色失败！');
					}
					closeDialog(createRoleDialog);
				});
			}
		}

	});
}
var chakancellfuncs = [ function(data) {
	return data.title;
}, function(data) {
	return data.tvalue;
} ];
// 列表数据处理
var cellfuncs = [
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				rootDialog.showdivWidth = 500;
				rootDialog.showdivHeight = 240;
				rootDialog.title = "查看角色信息";
				openDialog(rootDialog, '', function() {
					var chakantable = document.createElement("table");
					chakantable.style.align = "center";
					chakantable.className = "table";
					rootDialog.contentdiv.appendChild(chakantable);
					var chakantbody = document.createElement("tbody");
					chakantbody.setAttribute('id', 'chakantbody');
					chakantable.appendChild(chakantbody);
					Role.chakan(data.roleid, function(data) {
						DWRUtil.removeAllRows('chakantbody');
						DWRUtil.addRows('chakantbody', data, chakancellfuncs,
								tableclass);

					});
				});
			};
			href.innerHTML = data.rolename;
			return href;
		},
		function(data) {
			return data.roletext;
		},
		function(data) {
			return data.rolelevel;
		},
		function(data) {
			return data.sign == '1' ? '有效' : '无效';
		},
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				initSelectUsers(data.roleid, "role");
			};
			href.innerHTML = '成员列表';
			return href;
		},
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				initDept(data.roleid, "role");
			};
			href.innerHTML = '部门列表';
			return href;
		},
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				initSelectPopedoms(data.roleid, data.pop);
			};
			href.innerHTML = '权限列表';
			return href;
		},
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				openDialog(updateRoleDialog,
						URLParams['popBasePath'] + '/roles/add.htm', function() {
							initRole(data.roleid);
						});
			};
			href.innerHTML = '修改';
			return href;
		}, function(data) {
			// 创建一个删除连接
		var href = document.createElement('a');
		href.setAttribute('href', '#');
		// 给删除连接添加事件
		href.onclick = function() {
			if (confirm('确认删除角色(' + data.rolename + ')吗?')) {
				// 调用服务器删除方法
				Role.removeRoleById(data.roleid, function(data) {
					send(role);
					alert(data);
				});
			}
			;
		};
		href.innerHTML = '删除';
		return href;
	} ];
// 初始化分页参数
var role = {
	navigationsize :10,
	paramname :'role',
	page :1,
	funcname :'Role.findAllRolePage()',
	cellfuncs :cellfuncs,
	pagesize :10,
	count :'Role.getCount()',
	idname :'rolelistZone'
};
var pageuuid = role.paramname;
// 页面加载完成后初始化显示数据
window.onload = function() {
	initDWR();
	useLoading('../../expimages/blue-loading.gif', 'img');
	Role.checkPopAnd(8, function(data) {
		if (data) {
			send(role);
		} else {
			alert("请勿非法访问，仅拥有（权限管理）权限的用户可以访问！")
			top.window.location = URLParams['basePath'];
		}
	});
};